package com.example.rabbitspringboot.conf;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.PortUnreachableException;
import java.util.HashMap;

/**
 * @Authther: mrwu
 * @date:2021/5/13-05
 * @Description:com.example.rabbitspringboot.conf
 */
@Configuration
@EnableTransactionManagement //开启事务
public class AppConf {
    static final String EXCHANGE_NAME = "ex.order";
    static final String QUEUE_NAME="queue.order";
    //配置交换器


    @Bean
    public Exchange exchange(){

        return new DirectExchange(EXCHANGE_NAME,true,false,null);
    }
    @Bean
    public Queue queue(){

        HashMap<String,Object> args = new HashMap<>();
        args.put("x-message-ttl",3*1000);//设置消息过期时间
        //设置死信队列
        args.put("x-dead-letter-exchange", "ex.order.dlx");
        //设置死信交换器的routingkey
        args.put("x-dead-letter-routing-key","order.dlx");

        //队列持久化
        return new Queue(QUEUE_NAME, true, false, false, args);
    }

    @Bean
    public Binding binding(){

        return new Binding(QUEUE_NAME, Binding.DestinationType.QUEUE,EXCHANGE_NAME,"order",null);
    }

    @Bean
    public Exchange exchangeDlx(){
        DirectExchange exchange = new DirectExchange("ex.order.dlx",
                true, false, null);
        return exchange;
    }

    @Bean
    public Queue queueDlx(){
        return new Queue("queue.order.dlx");
    }

    @Bean
    public Binding bindingDlx(){
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("order.dlx").noargs();
    }

}
